<template>
  <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form inline :model="form" ref="form" class="demo-form-inline">
          <el-form-item prop="name" label="角色名称">
            <el-input v-model="form.name" placeholder="资源名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :disabled="isLoading"
              type="primary"
              @click="onSubmit">查询</el-button>
            <el-button
              :disabled="isLoading"
              @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button @click="dialogVisible = true">添加角色</el-button>
      <el-table :data="roles" style="width: 100%" v-loading="isLoading">
        <el-table-column type="index" label="编号" width="50">
        </el-table-column>
        <el-table-column prop="name" label="角色名称"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column prop="createdTime" label="添加时间"> </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="$router.push({
                name: 'alloc-menu',
                params: {
                  roleId: scope.row.id
                }
              })"
            >
              分配菜单
            </el-button>
            <el-button
              type="text"
              size="small"
            >
              分配资源
            </el-button>
            <br>
            <el-button
              @click="handleEdit(scope.row)"
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click="handleDelete(scope.$index, scope.row)"
              type="text"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :disabled="isLoading"
        :current-page.sync="form.current"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="form.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-dialog
      :title="isEdit ? '编辑角色' : '添加角色'"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <create-or-edit-role v-if="dialogVisible"
        :role-id="roleId"
        :is-edit="isEdit"
        @success="onSuccess"
        @cancel="dialogVisible = false"
      />
    </el-dialog>
    </el-card>
</template>

<script lang="ts">
import Vue from 'vue'
import { Form } from 'element-ui'
import { getRolePages } from '@/services/role'
import CreateOrEditRole from './components/CreateOrEditRole.vue'

export default Vue.extend({
  name: 'Role',
  components: {
    CreateOrEditRole
  },
  data () {
    return {
      roles: [],
      total: 0,
      form: {
        name: '',
        code: '',
        size: 5,
        current: 1
      },
      categroies: [],
      isLoading: true,
      dialogVisible: false,
      isEdit: false,
      roleId: null
    }
  },

  created () {
    this.loadRoles()
  },

  methods: {
    onReset () {
      (this.$refs.form as Form).resetFields()
      this.form.current = 1
      this.loadRoles()
    },
    onSubmit () {
      console.log('submit')
      this.form.current = 1
      this.loadRoles()
    },

    async loadRoles () {
      this.isLoading = true
      const { data } = await getRolePages(this.form)
      console.log(data)
      if (data.code === '000000') {
        const { records, total } = data.data
        this.roles = records
        this.total = total
        this.isLoading = false
      }
    },
    handleEdit (role: any) {
      // console.log('h')
      this.roleId = role.id
      this.isEdit = true
      this.dialogVisible = true
    },
    handleDelete () {
      console.log('h')
    },
    handleSizeChange (val: number) {
      this.form.current = 1
      this.form.size = val
      this.loadRoles()
    },
    handleCurrentChange (val: number) {
      this.form.current = val
      this.loadRoles()
    },

    handleClose () {
      this.dialogVisible = false
    },

    onSuccess () {
      this.dialogVisible = false
      // @todo 重新加载数据
    }
  }
})
</script>

<style lang="scss" scoped>
</style>
